FEAT: Complex Data Type Support- money & smallmoney#230
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for SQL Server MONEY and SMALLMONEY data types in the Python driver by implementing proper binding, encoding/decoding of decimal values, and comprehensive test coverage.
Key Changes:
- Enhanced decimal value handling to detect and bind MONEY/SMALLMONEY ranges as strings with fallback to numeric binding
- Improved decimal encoding in C++ bindings using little-endian byte arrays for SQL_NUMERIC_STRUCT compatibility
- Added robust NULL handling and error management for SQL DECIMAL/NUMERIC type conversions
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/test_004_cursor.py | Comprehensive test suite for MONEY/SMALLMONEY handling including boundary values, NULL handling, roundtrip conversion, and error cases |
| mssql_python/pybind/ddbc_bindings.cpp | Enhanced decimal encoding to little-endian byte arrays and improved NULL/error handling for SQL DECIMAL/NUMERIC types |
| mssql_python/cursor.py | Added MONEY/SMALLMONEY range detection with string binding for values within range, fallback to numeric binding otherwise |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
dc7f517 to
84d03bc
Compare
Thanks for the feedback! I have addressed the comments and I wanted to provide context for why we currently bind MONEY and SMALLMONEY as SQL_VARCHAR/SQL_C_CHAR rather than SQL_NUMERIC.
To avoid all this, we have bound these types as strings (SQL_VARCHAR/SQL_C_CHAR). SQL Server handles the implicit conversion from string to MONEY/SMALLMONEY on insert, and we can fetch numeric values as strings and then parse them to Python Decimal without losing precision. This approach also avoids integer overflow issue. |
Work Item / Issue Reference
Summary
This pull request enhances support for SQL Server MONEY and SMALLMONEY types in the Python driver, ensuring correct handling of boundary values, NULLs, and round-trip conversions. It also improves the encoding and decoding of decimal values and adds comprehensive tests to validate the new behavior.
MONEY and SMALLMONEY Type Handling:
_map_sql_typeincursor.pyto detect decimal values within MONEY and SMALLMONEY ranges and bind them as strings, falling back to generic numeric binding otherwise.Decimal Value Encoding/Decoding:
ddbc_bindings.cppby writing them as little-endian byte arrays, ensuring compatibility with SQL_NUMERIC_STRUCT.decimal.Decimal, with robust NULL and error handling during result fetching.Testing Enhancements:
test_004_cursor.pyto verify MONEY and SMALLMONEY handling, including: